/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CommonService;
import com.lmx.persistence.service.CustomerService;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.Customer;
import com.lmx.repoDomain.tmodel.NurseGroup;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiFindOne;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(NurseGroup.class)
@Component
public class NurseGroupRepository extends BaseRepository<NurseGroup> {

	/*
	 * @Autowired private CustomerService customerService;
	 */

	@Autowired
	private NursingService nursingService;

	@Autowired
	protected CommonService commonService;

	@Autowired
	protected CustomerService customerService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(NurseGroup.class);
	}

	// @Override
	// @JsonApiSave
	// public NurseGroup save(NurseGroup group) {
	// group = super.save(group);
	// // 建立 护工与老人对应关系 （关系）Employee--group--bed---customer
	// //customerService.buildEmployeeToCustomerByGroup(group);
	// return group;
	// }

	@Override
	@JsonApiSave
	public NurseGroup save(NurseGroup group) {
		if (group.getId() == null) {
			NurseGroup temp = nursingService.getNurseGroupByName(group.getName());
			if (temp == null) {
				group = super.save(group);
			} else {
				group.setErrcode(4);// 名字重复
				throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
			}
		} else {
			if (group.getDelStatus() == 1) {// 删除
				dslJpaService.update(group);
				nursingService.deleteNursrGroup(group.getId());
			} else {//
				int updateFlag = 1;
				NurseGroup oldData = nursingService.getNurseGroupById(group.getId());
				if (oldData.getName().equals(group.getName())) {

				} else {
					NurseGroup temp = nursingService.getNurseGroupByName(group.getName());
					if (temp == null) {

					} else {
						updateFlag = 0;
					}
				}

				if (updateFlag == 1) {
					group = super.save(group);
				} else {
					group.setErrcode(4);// 名字重复
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
				}
			}
		}
		return group;
	}

	@Override
	@JsonApiFindAll
	public Iterable<NurseGroup> findAll(final QueryParams requestParams) {
		Map<String, String> keyMap = CommonUtil.getKeyMapByFilter(requestParams);
		String queryType = keyMap.get("queryType");
		String typeName = "";
		List dataList = new ArrayList<>();
		Iterable<NurseGroup> groupList = new ArrayList<>();
		if (keyMap.get("typeName") != null) {
			typeName = keyMap.get("typeName");
		}
		if ("customer".equals(queryType)) {
			dataList = nursingService.getNurseGroupByCustomerName(typeName);
			commonService.setPageInfo(dataList, requestParams);
			groupList = dataList;
		} else if ("staff".equals(queryType)) {
			dataList = nursingService.getNurseGroupByStaffName(typeName);
			commonService.setPageInfo(dataList, requestParams);
			groupList = dataList;
		} else { 
			dslJpaService.setEntityClass(NurseGroup.class);
			groupList = super.findAll(requestParams);// getCustomerByGroup
		}
		for(NurseGroup group:groupList){
		  List<Customer> customerList=new ArrayList<>();
		  customerList=customerService.getCustomerByGroup(group);
		  group.setCustomers(customerList);
		}
		return groupList;
	}
	
	@Override
	@JsonApiFindOne
	public NurseGroup findOne(final Long id, final QueryParams requestParams) {
		NurseGroup group = null;
		group = (NurseGroup) dslJpaService.findById(id);
		List<Customer> customerList = new ArrayList<>();
		customerList = customerService.getCustomerByGroup(group);
		group.setCustomers(customerList);
		return group;
	}

}
